<?php
namespace App\Service\Admin;

class StaffService extends BaseService
{
    //列表
    public static function index($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbStaff)->where('id', '>', 1);
        if ($pas['role_id'] && $pas['role_id']>2) {
            $db = $db->where('role_id', $pas['role_id']);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '员工不存在');
        }
        $arys = [];
        foreach ($res as $v) {
            $roleOne = self::dbConn(self::$dbStRole)
                ->where('id', $v->role_id)
                ->first();
            $role_name = empty($roleOne) ? '' : $roleOne->title;
            $shopOne = self::dbConn(self::$dbYwShop)
                ->where('id', $v->shop_id)
                ->first();
            $shop_name = empty($shopOne) ? '' : $shopOne->title;
            $head = substr($v->head, 0, 4)=='http' ? $v->head : env('DOMAIN').'/'.$v->head;
            $arys[] = [
                'id' => $v->id, 'title' => $v->title, 'head' => $head,
                'role_id' => $v->role_id, 'role_name' => $role_name,
                'shop_id' => $v->shop_id, 'shop_name' => $shop_name,
                'lasttime' => $v->uptime, 'is_able' => $v->is_able,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //选项
    public static function getOption()
    {
        $arys = ['role' => [], 'shop' => []];
        $arys['role'] = self::dbConn(self::$dbStRole)
            ->select('id', 'title')
            ->where('del', 0)
            ->get();
        $arys['shop'] = self::dbConn(self::$dbYwShop)
            ->select('id', 'title')
            ->get();
        return self::toback(0, '', $arys);
    }

    //新增
    public static function add($pas)
    {
        $pas['addtime'] = $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbStaff)
            ->where('title', $pas['title'])
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '存在同名数据');
        }
        $res = self::dbConn(self::$dbStaff)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //新增
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbStaff)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbStaff)->where('id', $id)->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

//    //删除
//    public static function del($id)
//    {
//        $res = self::dbConn(self::$dbStaff)
//            ->where('del', 0)
//            ->where('id', $id)
//            ->first();
//        if (empty($res)) {
//            return self::toback(-2, '没有数据');
//        }
//        $res = self::dbConn(self::$dbStaff)
//            ->where('id', $id)
//            ->update(['del' => 1, 'deltime' => toDate()]);
//        if (!$res) {
//            return self::toback(-1, 'error');
//        }
//        return self::toback();
//    }

//    //删除
//    public static function del($id)
//    {
//        $res = self::dbConn(self::$dbStaff)
//            ->where('del', 0)
//            ->where('id', $id)
//            ->first();
//        if (empty($res)) {
//            return self::toback(-2, '没有数据');
//        }
//        $res = self::dbConn(self::$dbStaff)
//            ->where('id', $id)
//            ->update(['del' => 1, 'deltime' => toDate()]);
//        if (!$res) {
//            return self::toback(-1, 'error');
//        }
//        return self::toback();
//    }

    //启用禁用
    public static function toable($id, $able)
    {
        $res = self::dbConn(self::$dbStaff)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        if ($res->is_able == $able) {
            return self::toback(-2, '无需修改');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbStaff)
            ->where('id', $id)
            ->update(['is_able' => $able, 'uptime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //重置密码
    public static function reset($id)
    {
        $res = self::dbConn(self::$dbStaff)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $res = self::dbConn(self::$dbStaff)
            ->where('id', $id)
            ->update(['passwd' => md5('123456'), 'uptime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //修改密码
    public static function pwdModify($id, $pwd, $pwd2)
    {
        $res = self::dbConn(self::$dbStaff)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        if ($res->passwd != md5($pwd)) {
            return self::toback(-2, '原密码错误');
        }
        $res = self::dbConn(self::$dbStaff)
            ->where('id', $id)
            ->update(['passwd' => md5($pwd2), 'uptime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    /**
     * =======================
     * 一下是登录操作
     * =======================
     */

    //登录
    public static function dologin($uname, $upwd)
    {
        $loginMsg = ['uname' => $uname, 'upwd' => $upwd];
        $uOne = self::dbConn(self::$dbStaff)
            ->where('title', $uname)
            ->first();
        if (empty($uOne)) {
            self::addLog($loginMsg, 0, '员工不存在');
            return self::toback(-2, '员工不存在');
        }
        if ($uOne->is_able > 0) {
            self::addLog($loginMsg, 0, '员工被禁用');
            return self::toback(-2, '员工被禁用');
        }
        if ($uOne->passwd != md5($upwd)) {
            self::addLog($loginMsg, $uOne->id, '密码错误');
            return self::toback(-2, '密码错误');
        }
        if ($uOne->is_able) {
            self::addLog($loginMsg, $uOne->id, '员工已被禁用');
            return self::toback(-2, '员工已被禁用');
        }
        self::addLog($loginMsg, $uOne->id, '登录成功');
        $roleOne = self::dbConn(self::$dbStRole)
            ->where('id', $uOne->role_id)
            ->first();
        $role_name = empty($roleOne) ? '' : $roleOne->title;
        $ary = [
            'uid' => $uOne->id, 'uname' => $uname,
            'shop_id' => $uOne->shop_id,
            'role_id' => $uOne->role_id,
            'role_name' => $role_name,
        ];
        return self::toback(0, '', $ary);
    }

    //登录日志
    public static function addLog($loginMsg = [], $uid = 0, $errMsg = '')
    {
        //$loginMsg = json_encode($loginMsg, 384);
        $loginMsg = json_encode($loginMsg, JSON_UNESCAPED_UNICODE);
        return self::dbConn(self::$dbStLogin)->insertGetId([
            'staff_id' => $uid, 'loginMsg' => $loginMsg,
            'errMsg' => $errMsg, 'addtime' => toDate(),
        ]);
    }

    //UID获取权限
    public static function leftMenu($uid, $role_id)
    {
        //获取角色
        $uOne = self::dbConn(self::$dbStaff)->where('id', $uid)->first();
        if (!$uOne) {
            return self::toback(-2, '员工不存在');
        }
        if (!$uOne->role_id) {
            return self::toback(-2, '员工角色不存在');
        }
        $act_ids = []; //权限ID集合
        $authOnes = self::dbConn(self::$dbStAuth)
            ->where('del', 0)
            ->where('role_id', $role_id)
            ->get();
        if (!count($authOnes)) {
            return self::toback(-2, '权限不存在');
        }
        foreach ($authOnes as $v) {
            $act_ids[] = $v->action_id;
            $actOne = self::dbConn(self::$dbStAction)
                ->where('id', $v->action_id)
                ->first();
            if (!empty($actPone) || !in_array($actOne->pid, $act_ids)) {
                $act_ids[] = $actOne->pid;
            }
        }
        if (!count($act_ids)) {
            return self::toback(-2, '没有权限');
        }
        //获取权限
        $actOnes = self::dbConn(self::$dbStAction)
            ->whereIn('id', $act_ids)
            ->where('pid', 0)
            ->get();
        if (!count($actOnes)) {
            return self::toback(-2, '菜单不存在');
        }
        $actArys = []; //权限ID集合
        foreach ($actOnes as $v) {
            $childs = self::dbConn(self::$dbStAction)
                ->whereIn('id', $act_ids)
                ->where('pid', $v->id)
                ->orderBy('sort', 'desc')
                ->get();
            $childList = [];
            if (count($childs)) {
                foreach ($childs as $vv) {
                    $childList[] = [
                        'id' => $vv->id, 'title' => $vv->title,
                        'link' => $vv->link, 'pid' => $vv->pid,
                    ];
                }
            }
            if ($v->id == 1) { $icon = '&#xe62e;'; }
            else { $icon = '&#xe616;'; }
            $actArys[] = [
                'id' => $v->id, 'title' => $v->title,
                'link' => $v->link, 'childs' => $childList,
                'icon' => $icon,
            ];
        }
        //获取项目配置
        $configObj = file_get_contents('config.json');
        if (!$configObj) {
            return self::toback(-2, '配置错误');
        }
        $config = json_decode($configObj, true);
        $config['guoqi'] = strtotime($config['period']) < time() ? false : true;
        $ary = [
            'act' => $actArys,
            'config' => $config,
        ];
        return self::toback(0, '', $ary);
    }
}